package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Random;

import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactModificationTests extends TestBase {
	
	@Test
	public void modifySomeContactWay1(){
		// save old state
		SortedListOf<ContactData> oldList = app.getContactHelper().getContacts();		
		//actions
		Random rnd = new Random();
		int index = rnd.nextInt(oldList.size() - 1);
		ContactData contact = new ContactData()
		.withLastName(generateRandomString())
		.withFirstName(generateRandomString());
		
		app.getContactHelper().modifyContactWay1(index,contact);
		// save new state
		SortedListOf<ContactData> newList = app.getContactHelper().getContacts();
		
		assertThat(newList, equalTo(oldList.without(index).withAdded(contact)));
		//----old variant
		//oldList.remove(index);
		//oldList.add(contact);
		//assertEquals(newList, oldList);


	}
	@Test
	public void modifySomeContactWay2(){
		// save old state
		SortedListOf<ContactData> oldList = app.getContactHelper().getContacts();		
		//actions	
		Random rnd = new Random();
		int index = rnd.nextInt(oldList.size() - 1);
		ContactData contact = new ContactData()
		.withLastName(generateRandomString())
		.withFirstName(generateRandomString());
		
		app.getContactHelper().modifyContactWay2(index, contact);
		
		// save new state
		SortedListOf<ContactData> newList = app.getContactHelper().getContacts();
		assertThat(newList, equalTo(oldList.without(index).withAdded(contact)));
		// -----old variant
		//oldList.remove(index);
		//oldList.add(contact);
		//assertEquals(newList, oldList);		
	}	
}
